package where2go.sctu.com.zhiyou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.adpater.StudyAdaper;
import where2go.sctu.com.zhiyou.db.ScrollLinearLayoutManager;
import where2go.sctu.com.zhiyou.entity.StudyData;
import where2go.sctu.com.zhiyou.utils.HeaderStringRequest;
import where2go.sctu.com.zhiyou.utils.ShareUtils;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.activity
 * Created by 张某人
 * CreatedTime:on 2018/5/24   22:23
 * Description:
 */

public class MyStudyActivity extends AppCompatActivity {
    private ImageView imageView;
    private RecyclerView studyRecyclerView;
    private StudyAdaper mAdapter;

    private List<StudyData.DataBean> dataBeanList=new ArrayList<StudyData.DataBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_study_fragment);

        initView();
    }

    private void initView() {

        studyRecyclerView=(RecyclerView) findViewById(R.id.study_recyclerView);
        imageView=(ImageView) findViewById(R.id.study);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        studyRecyclerView.setLayoutManager(new ScrollLinearLayoutManager(MyStudyActivity.this,
                ScrollLinearLayoutManager.VERTICAL,false));
        getMyCourse();
    }

    private void getMyCourse(){
        String url="http://47.104.80.155/youyiyou/userclass/show";
        HeaderStringRequest request=new HeaderStringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
                StudyData data=gson.fromJson(response,StudyData.class);
                dataBeanList=data.getData();
                mAdapter=new StudyAdaper(MyStudyActivity.this,dataBeanList);
                mAdapter.setItemClickListener(new StudyAdaper.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        //跳转到详情页
                        Intent intent=new Intent();
                        intent.setClass(MyStudyActivity.this,
                                LineDetailsActivity.class);
                        //用Bundle携带数据
                        Bundle bundle=new Bundle();
                        int classId=dataBeanList.get(position).getAClass().getClassId();
                        intent.putExtra("classId",classId);
                        bundle.putString("classId", String.valueOf(classId));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }

                    @Override
                    public void onItemLongClick(View view, int position) {

                    }
                });
                studyRecyclerView.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.toString());
                Toast.makeText(MyStudyActivity.this, "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> map=new HashMap<String,String>();
                map.put("authorization", ShareUtils.getString(MyStudyActivity.this,"token",null));
                return map;
            }
        };

        MyApplication.getHttpQueues().add(request);
    }
}
